require 'gtk2'
require "yaml"
require "config.rb"
require "plugins"
class Window
# initialization {{{
	@@nWindow=0
  @@bindkeys={}
  begin
    @@bindkeys=YAML::load(File.open(ENV['HOME']+File::SEPARATOR+".rviewrc"))
  rescue 
    include Config
    @@bindkeys=Config::BINDKEYS
  end
include Plugins
	def initialize(fileList,filePos,zoom=1,slides=false,slideShowDelay=5000)
		@@nWindow+=1
		@time=0
		@fileList=fileList
		@filePos=filePos
#		<image>
		@zoom=zoom
		@xpos=0
		@ypos=0
#		<image/>
		@moveStep=0.10
		@wc=0
		@hc=0
		@slideShow=slides
		@slideShowDelay=slideShowDelay
		@random=false
    @tags=[]
#		<image>
                picture
		@imageScale= Gtk::Image.new @pixbuf
#		<image/>
    title="rview -"
    title='rview - '+@fileList[@filePos] if not @fileList[@filePos].nil?
		@win=Gtk::Window.new(title)
		vbox=Gtk::VBox.new(false,0)
		@text=Gtk::Entry.new
		@text.has_frame=false
		vbox.pack_end @imageScale
		@win.allow_shrink=true
		@win.modify_bg(Gtk::STATE_NORMAL,Gdk::Color.new(0,0,0))
		@win.add vbox
		@win.show_all
		@win.signal_connect('size-allocate') {resize()}
		@win.signal_connect('key-press-event') {|*event| keyPress event[1] }
	end
#}}}
  def picture #{{{
    if @fileList.empty?
      @pixbuf = Gdk::Pixbuf.new(Gdk::Pixbuf::COLORSPACE_RGB,false,8,800,600) 
      @pixbuf.fill!(0x00000000)
      return
    end
    begin
      @pixbuf = Gdk::Pixbuf.new @fileList[@filePos]
    rescue 
      file_plugin 
    rescue
    @fileList.delete_at(@filePos)
    @filePos=0 if @filePos>@fileList.size-1
    Gtk.main_quit if @fileList.empty?
    picture
    end
  end
  #}}}
	def resize #{{{
		  #snooper=Gtk.key_snooper_install {|win,event| true}
			w=@win.allocation.width.to_f
			h=@win.allocation.height.to_f
			if w!=@wc or h!=@hc
#		<image>
			pixWidth=@pixbuf.width.to_f
			pixHeight=@pixbuf.height.to_f
#		<image>
			@wc=w
			@hc=h
#		<image>
			scale=[w/pixWidth,h/pixHeight].min
			scale=scale*@zoom
			if @zoom<=1
				@imageScale.pixbuf=@pixbuf.scale(scale*pixWidth,scale*pixHeight,Gdk::Pixbuf::INTERP_BILINEAR)
			else
				tmpPixWidth=[pixWidth,@wc/scale].min
				tmpPixHeight=[pixHeight,@hc/scale].min
				@xpos=pixWidth/2-tmpPixWidth/2 if (pixWidth/2-tmpPixWidth/2+@xpos)>(pixWidth-tmpPixWidth)
				@ypos=pixHeight/2-tmpPixHeight/2 if (pixHeight/2-tmpPixHeight/2+@ypos)>(pixHeight-tmpPixHeight)
				@xpos=-pixWidth/2+tmpPixWidth/2 if (pixWidth/2-tmpPixWidth/2+@xpos)<0
				@ypos=-pixHeight/2+tmpPixHeight/2 if (pixHeight/2-tmpPixHeight/2+@ypos)<0
				x=pixWidth/2-tmpPixWidth/2+@xpos
				y=pixHeight/2-tmpPixHeight/2+@ypos
				tmpPixbuf=Gdk::Pixbuf.new(@pixbuf,x,y,tmpPixWidth,tmpPixHeight)
				@imageScale.pixbuf=tmpPixbuf.scale(scale*tmpPixWidth,scale*tmpPixHeight,Gdk::Pixbuf::INTERP_BILINEAR)
			end
#		<image/>
			Gtk.main_iteration
			end
			GC.start
		  #Gtk.key_snooper_remove snooper
	end
#}}}
	def nextSlide #{{{
		if @random
			@filePos=rand(@fileList.size)
		else
			@filePos+=1
		end
		@filePos=0 if @filePos>@fileList.size-1
#		@pixbuf = Gdk::Pixbuf.new @fileList[@filePos]
                picture
		GC.start
		@imageScale.pixbuf= @pixbuf
		@zoom=1
    if not @fileList[@filePos].nil?
      @win.title='rview - '+@fileList[@filePos]
    else
      @win.title='rview - '
    end
		@wc=0
		@wh=0
		@xpos=0
		@ypos=0
		resize
	end
#}}}
	def prevSlide #{{{
		@filePos-=1
		@filePos=@fileList.size-1 if @filePos<0 
#		@pixbuf = Gdk::Pixbuf.new @fileList[@filePos]
                picture
		GC.start
		@imageScale.pixbuf = @pixbuf
		@zoom=1
    if not @fileList[@filePos].nil?
      @win.title='rview - '+@fileList[@filePos]
    else
      @win.title='rview - '
    end
		@wc=0
		@wh=0
		@xpos=0
		@ypos=0
		resize
	end
#}}}
	def zoomIn #{{{
		@zoom=@zoom*1.2
		@wc=0
		@wh=0
		resize
	end
#}}}
	def zoomOut #{{{
		@zoom=@zoom/1.2
		@wc=0
		@wh=0
		resize
	end
#}}}
	def move x,y #{{{
		pixWidth=@pixbuf.width.to_f
		pixHeight=@pixbuf.height.to_f
		@xpos+=pixWidth/@zoom*x
		@ypos+=pixHeight/@zoom*y
		@wc=0
		@wh=0
		resize
	end
#}}}
	def slideShow #{{{
		case @slideShow
			when true
				@slideShow=false
				Gtk.timeout_remove(@slideTimeoutId)
			when false
				@slideShow=true
				nextSlide
				@slideTimeoutId=Gtk.timeout_add(@slideShowDelay){ nextSlide; true }
			end
	end
# }}}
	def destroy #{{{
		Gtk.timeout_remove(@slideTimeoutId) if @slideShow	
		@win.destroy
		# I dont know how to clean the Gtk garbage properly
		@win=nil
		@pixbuf=nil
		@imageScale=nil
		GC.start
		@@nWindow-=1
		Gtk.main_quit if @@nWindow==0
	end
#}}}
	def keyPress (event) #{{{
		return if (event.time-@time)<150
		@time=event.time
		ev_command=@@bindkeys[Gdk::Keyval::to_name(event.keyval)]
    if ev_command.nil?
      $stderr.puts(Gdk::Keyval::to_name(event.keyval)) if $DEBUG
      return
    else
      eval ev_command
    end
	end
#}}}
  def command(command=nil) #{{{
  command=`dmenu #{Config::DMENU_OPTIONS} -p "$: " <<EOF
EOF
    ` if command.nil?
    command.gsub!("\#",@fileList[@filePos])
    command.gsub!("\*",@fileList*" ")
    command.gsub!("\!",@tags*" ")
    out = `#{command}` if command!=""
    puts out
  end
  #}}}
  def win_eval(command=nil) #{{{
  command=`dmenu #{Config::DMENU_OPTIONS} -p "ruby: " <<EOF
reload
@fileList
@tags
@slideShowDelay=
@zoom=
@xpos=
@ypos=
@moveStep=
#{Config::public_instance_methods*"\n"}
EOF
    ` if command.nil?
    out = eval command
    puts out
		@wc=0
		@wh=0
    resize
  end
  #}}}
  def tag(file) #{{{
    if @tags&[@fileList[@filePos]]==[] 
      @tags << @fileList[@filePos]
    else
      @tags-=[@fileList[@filePos]] 
    end
  end
  #}}}
  def reload #{{{
    picture
    GC.start
    @imageScale.pixbuf= @pixbuf
    @zoom=1
    @win.title='rview - '+@fileList[@filePos]
    @wc=0
    @wh=0
    @xpos=0
    @ypos=0
    resize
  end
  #}}}

end

